package com.zqx.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zqx.domain.Dorm;
import com.zqx.domain.Supervisor;
import com.zqx.service.DormService;
import com.zqx.service.SupervisorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Controller
@RequestMapping("super")
public class SupervisorController {
    @Autowired
    private SupervisorService supervisorService;
    @Autowired
    private DormService dormService;
    @Value("${location}")
    private String location;

    //查询全部宿管信息
    @RequestMapping("listSuper")
    public String listDorm(@RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
                           @RequestParam(value = "pageSize", defaultValue = "4", required = false) Integer pageSize, Supervisor supervisor, Model model) {
        if (pageNum < 0 || pageNum.equals("") || pageNum == null) {
            pageNum = 1;
        }
        if (pageNum < 0 || pageNum.equals("") || pageNum == null) {
            pageSize = 4;
        }
        //分页
        PageHelper.startPage(pageNum, pageSize);
        //条件构造器：MP用来查询一些特殊的字段
        //模糊查询
        QueryWrapper<Supervisor> qw = new QueryWrapper<>();
        if (supervisor.getSname() != null) {
            qw.like("sname", supervisor.getSname());
        }

        List<Supervisor> list = supervisorService.list(qw);
        PageInfo<Supervisor> pageInfo = new PageInfo<>(list);
        model.addAttribute("pageInfo", pageInfo);
        //根据宿舍信息查询所有楼层来做复选框
        List<Dorm> list1 = dormService.list(null);
        Set<String> set = new HashSet<>();
        for (Dorm dorm : list1) {
            set.add(dorm.getBuild());
        }
        model.addAttribute("buildList", set);
        return "admin-super-list";
    }

    @RequestMapping("saveSuper")
    public String saveSuper(Supervisor supervisor, MultipartFile file) {
        if (!file.isEmpty()) {
            transfile(supervisor, file);
        }
        boolean save = supervisorService.save(supervisor);
        return "redirect:/super/listSuper";
    }
    //上传图片方法
    private void transfile(Supervisor supervisor, MultipartFile file) {
        String originalFilename = file.getOriginalFilename();//获取文件名
        int index = originalFilename.lastIndexOf(".");//切割文件，分成前、后缀
        String suffix = originalFilename.substring(index);//获取后缀
        String prefix = System.nanoTime() + "";//用时间来设置前缀
        String path = prefix + suffix;
        File file1 = new File(location);
        if (!file1.exists()) {
            file1.mkdirs();
        }
        File file2 = new File(file1, path);
        try {
            file.transferTo(file2);
        } catch (IOException e) {
            e.printStackTrace();
        }
        supervisor.setSimage(path);
    }
    //回显数据
    @RequestMapping("preUpdateSuper/{id}")
    public String preUpdateSuper(@PathVariable Integer id, Model model){
        Supervisor byId = supervisorService.getById(id);
        model.addAttribute("supervisor", byId);
        List<Dorm> list1 = dormService.list(null);
        Set<String> set = new HashSet<>();
        for (Dorm dorm : list1) {
            set.add(dorm.getBuild());
        }
        model.addAttribute("buildList", set);
        return "admin-super-update";
    }
    //修改宿管信息
    @RequestMapping("updateSuper")
    public String updateSuper (Supervisor supervisor, MultipartFile file){
        if (!file.isEmpty()){
            transfile(supervisor, file);
        }
        boolean b = supervisorService.updateById(supervisor);
        return "redirect:/super/listSuper";
    }
    //删除宿管信息
    @RequestMapping("delSuper/{id}")
    public String delStudent(@PathVariable Integer id){
        supervisorService.removeById(id);
        return "redirect:/super/listStudent";
    }
    //批量删除学生信息
    @ResponseBody
    @RequestMapping("batchDeleteSuper")
    public String batchDeleteStudent(String idList){
        String[] split = StrUtil.split(idList, ",");
        List<Integer> list = new ArrayList<>();
        for (String s:split
        ) {
            if (!s.isEmpty()){
                int i = Integer.parseInt(s);
                list.add(i);
            }
        }
        boolean b = supervisorService.removeByIds(list);
        if (b){
            return "OK";
        }else {
            return "error";
        }
    }
}

